package com.example.demo.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 类描述:
 *
 * @author zhanglei
 * @date 2019/3/8 10:40
 **/
@Configuration
@RefreshScope
public class DataBaseConfiguration {

    private Integer defaultDataSourceID = null;

    @Bean
    @RefreshScope
    public HashMap<Integer, DataSource> dataSourceMap(DataBaseHolder dataBaseHolder) {

        HashMap<Integer, DataSource> dataSourceMap = new HashMap<>();

        List<DataBaseInfo> dataBaseInfos = dataBaseHolder.getDatabaseList();
        if(CollectionUtils.isEmpty(dataBaseInfos)){
            return dataSourceMap;
        }

        for (DataBaseInfo database : dataBaseInfos) {

            if(defaultDataSourceID == null &&
                    database.getDataBaseType().equals(DataBaseInfo.DATABASE_TYPE_PRIMARY)){
                defaultDataSourceID = database.getDbId();
            }

            DataSource datasource = new DataSource();
            datasource.setPoolProperties(database);
            datasource.setDriverClassName("com.mysql.jdbc.Driver");
            dataSourceMap.put(database.getDbId(), datasource);
        }
        return dataSourceMap;
    }


    @Bean("dynamicDataSource")
    @Primary
    @RefreshScope
    public javax.sql.DataSource dynamicDataSource(Map<Integer, DataSource> dataSourceMap) {
        DynamicDataSource dynamicRoutingDataSource = new DynamicDataSource();

        Map<Object,Object> dynamicDataSourceMap = new HashMap<>();
        dynamicDataSourceMap.putAll(dataSourceMap);

        // 将 primary 数据源作为默认指定的数据源
        dynamicRoutingDataSource.setDefaultTargetDataSource(dataSourceMap.get(defaultDataSourceID));

        dynamicRoutingDataSource.setTargetDataSources(dynamicDataSourceMap);

        return dynamicRoutingDataSource;
    }

    @Bean
    @RefreshScope
    public SqlSessionFactory sqlSessionFactory(
            @Qualifier("dynamicDataSource") javax.sql.DataSource dynamicDataSource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dynamicDataSource);

        bean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath*:mapper/**/*.xml"));
//        bean.setConfigLocation(new PathMatchingResourcePatternResolver().getResource("classpath:mybatis-config.xml"));
        return bean.getObject();

    }

    @Bean(name = "sqlSessionTemplate")
    @RefreshScope
    public SqlSessionTemplate sqlSessionTemplate(
            @Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory)
            throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    @Bean(name = "invoiceJdbcTemplate")
    @RefreshScope
    public JdbcTemplate invoiceJdbcTemplate(@Qualifier("dynamicDataSource") javax.sql.DataSource dynamicDataSource) {
        return new JdbcTemplate(dynamicDataSource);
    }
}
